package com.sy.musicapp.ui.home

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.*
import androidx.compose.ui.unit.dp
import com.sy.musicapp.model.Music
import com.sy.musicapp.model.MusicViewModel
import com.sy.musicapp.ui.common.MusicListItem
import com.sy.musicapp.ui.common.MyCard

/**
 * 推荐歌曲卡片组件
 * @author SY
 * @since 2022-02-07 21:45
 **/
@ExperimentalAnimationApi
@Composable
fun RecommendCard(
    recomMusics: List<Music>,
    musicModel: MusicViewModel
) {
    val currentIndex by musicModel.currentMusicIndex.collectAsState()
    var currentPlay: Music by remember {
        mutableStateOf(Music.Empty)
    }
    LaunchedEffect(currentIndex) {
        currentPlay = musicModel.getCurrentMusic()
    }
    MyCard(
        title = "推荐",
        littleContent = { /*换一换*/ },
        content = {
            LazyColumn(
                contentPadding = PaddingValues(10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
                state = rememberLazyListState()
            ) {
                itemsIndexed(recomMusics,
                    { index: Int, _: Music -> index }) { index: Int, item: Music ->
                    MusicListItem(
                        music = item,
                        action = currentPlay,
                        isShowImgOnDefault = true,
                        itemClick = {
                            musicModel.addAllToMusicList(recomMusics)
                            musicModel.start(index)
                        },
                        itemIconClick = {
                            musicModel.addNextToMusicList(item)
                        }
                    )
                }
            }
        }
    )
}